package com.xhsj.user.hr.system.service;

import com.xhsj.user.config.UploadPathConfig;
import com.xhsj.user.hr.system.entity.TbHrSystemDocument;
import com.xhsj.user.hr.system.mapper.TbHrSystemDocumentMapper;
import com.xhsj.user.hr.system.service.TbHrSystemDocumentService;
import com.xhsj.user.hr.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 体系文件 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since Sep 15, 2020 1:06:25 PM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbHrSystemDocument")
public class TbHrSystemDocumentService extends BaseService<TbHrSystemDocumentMapper, TbHrSystemDocument>{
    @Autowired
    private TbHrSystemDocumentMapper dao;

    @Autowired
    private UploadPathConfig uploadPath;

	/**
	 * 保存
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbHrSystemDocument", allEntries=true)
    public boolean save(TbHrSystemDocument entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbHrSystemDocument getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbHrSystemDocument", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbHrSystemDocument> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbHrSystemDocument entity = JSON.parseObject(JSON.toJSONString(data), TbHrSystemDocument.class);
        Page<TbHrSystemDocument> page = new Page(currentPage,pageSize);
        QueryWrapper<TbHrSystemDocument> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author suxiaolin
	 * @date Sep 15, 2020 1:06:25 PM
	 */
    @Override
    //@Cacheable(value = "TbHrSystemDocument",sync = true)
    public List<TbHrSystemDocument> selectAll() {
        return super.selectAll();
    }


    /**
     * 物理删除记录
     * @author 苏小林
     * @date 2020/9/15 13:26
     * @param: id
     */
    public void delById(Long id,String path) throws Exception {
        dao.delById(id);
        path = uploadPath.getPath() + "/" + path.substring(path.lastIndexOf("download/") + 9);
        Path filePath = Paths.get(path);
        File toFile = filePath.toFile();
        boolean exists = toFile.exists();
        if (exists) {
            Files.delete(filePath);
        }else {
            throw new Exception("文件不存在");
        }
//        Files.delete(Paths.get(uploadPath.getTemp() + "/" + path));
    }
}
